﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Common;
using System.Data;

/// <summary>
/// Summary description for ShoppingCartAccess
/// </summary>
public class ShoppingCartAccess
{
	public ShoppingCartAccess()
	{
		//
		// TODO: Add constructor logic here
		//
	}

	// returns the shopping cart ID for the current user
	private static string shoppingCartId
	{
		get
		{
			// get the current HttpContext
			HttpContext context = HttpContext.Current;
			// try to retrieve the cart ID from the user cookie
			//string cartId = context.Request.Cookies["ElegantFood_CartID"].Value;
			string cartId;
			// if the cart ID isn't in the cookie...
			{
				// check if the cart ID exists as a cookie
				if (context.Request.Cookies["ElegantFood_CartID"] != null)
				{
					cartId = context.Request.Cookies["ElegantFood_CartID"].Value;
					// return the id
					return cartId;
				}
				else
				// if the cart ID doesn't exist in the cookie as well, generate
				// a new ID
				{
					// generate a new GUID
					cartId = Guid.NewGuid().ToString();
					// create the cookie object and set its value
					HttpCookie cookie = new HttpCookie("ElegantFood_CartID", cartId);
					// set the cookie's expiration date
					int howManyDays = ElegantFoodConfiguration.CartPersistDays;
					DateTime currentDate = DateTime.Now;
					TimeSpan timeSpan = new TimeSpan(howManyDays, 0, 0, 0);
					DateTime expirationDate = currentDate.Add(timeSpan);
					cookie.Expires = expirationDate;
					// set the cookie on the client's browser
					context.Response.Cookies.Add(cookie);
					// return the CartID
					return cartId.ToString();
				}
			}
		}
	}

	// Add a new shopping cart item
	public static bool AddItem(string productId)
	{
		// get a configured DbCommand object
		DbCommand cmd = GenericDataAccess.CreateCommand();
		// set the stored procedure name
		cmd.CommandText = "EF_ShoppingCartAddItem";
		// create a new parameter
		DbParameter param = cmd.CreateParameter();
		param.ParameterName = "@CartID";
		param.Value = shoppingCartId;
		param.DbType = DbType.String;
		param.Size = 36;
		cmd.Parameters.Add(param);

		// create a new parameter
		param = cmd.CreateParameter();
		param.ParameterName = "@ProductID";
		param.Value = productId;
		param.DbType = DbType.Int32;
		cmd.Parameters.Add(param);

		// returns true in case of success and false in case of an error
		try
		{
			// execute the stored procedure and return true if it executes
			// successfully, and false otherwise
			return (GenericDataAccess.ExecuteNonQuery(cmd) != -1);
		}
		catch
		{
			// prevent the exception from propagating, but return false to
			// signal the error
			return false;
		}
	}

	// Update the quantity of a shopping cart item
	public static bool UpdateItem(string productId, int quantity)
	{
		// get a configured DbCommand object
		DbCommand cmd = GenericDataAccess.CreateCommand();
		// set the stored procedure name
		cmd.CommandText = "EF_ShoppingCartUpdateItem";
		// create a new parameter
		DbParameter param = cmd.CreateParameter();
		param.ParameterName = "@CartID";
		param.Value = shoppingCartId;
		param.DbType = DbType.String;
		param.Size = 36;
		cmd.Parameters.Add(param);
		// create a new parameter
		param = cmd.CreateParameter();
		param.ParameterName = "@ProductID";
		param.Value = productId;
		param.DbType = DbType.Int32;
		cmd.Parameters.Add(param);
		// create a new parameter
		param = cmd.CreateParameter();
		param.ParameterName = "@Quantity";
		param.Value = quantity;
		param.DbType = DbType.Int32;
		cmd.Parameters.Add(param);
		// returns true in case of success and false in case of an error
		try
		{
			// execute the stored procedure and return true if it executes
			// successfully, and false otherwise
			return (GenericDataAccess.ExecuteNonQuery(cmd) != -1);
		}
		catch
		{
			// prevent the exception from propagating, but return false to
			// signal the error
			return false;
		}
	}

	// Remove a shopping cart item
	public static bool RemoveItem(string productId)
	{
		// get a configured DbCommand object
		DbCommand cmd = GenericDataAccess.CreateCommand();
		// set the stored procedure name
		cmd.CommandText = "EF_ShoppingCartRemoveItem";
		// create a new parameter
		DbParameter param = cmd.CreateParameter();
		param.ParameterName = "@CartID";
		param.Value = shoppingCartId;
		param.DbType = DbType.String;
		param.Size = 36;
		cmd.Parameters.Add(param);
		// create a new parameter
		param = cmd.CreateParameter();
		param.ParameterName = "@ProductID";
		param.Value = productId;
		param.DbType = DbType.Int32;
		cmd.Parameters.Add(param);
		// returns true in case of success and false in case of an error
		try
		{
			// execute the stored procedure and return true if it executes
			// successfully, and false otherwise
			return (GenericDataAccess.ExecuteNonQuery(cmd) != -1);
		}
		catch
		{
			// prevent the exception from propagating, but return false to
			// signal the error
			return false;
		}
	}

	// Retrieve shopping cart items
	public static DataTable GetItems()
	{
		// get a configured DbCommand object
		DbCommand cmd = GenericDataAccess.CreateCommand();
		// set the stored procedure name
		cmd.CommandText = "EF_ShoppingCartGetItems";
		// create a new parameter
		DbParameter param = cmd.CreateParameter();
		param.ParameterName = "@CartID";
		param.Value = shoppingCartId;
		param.DbType = DbType.String;
		param.Size = 36;
		cmd.Parameters.Add(param);
		// return the result table
		DataTable table = GenericDataAccess.ExecuteSelectCommand(cmd);
		return table;
	}

	// Retrieve shopping cart items
	public static decimal GetTotalAmount()
	{
		// get a configured DbCommand object
		DbCommand cmd = GenericDataAccess.CreateCommand();
		// set the stored procedure name
		cmd.CommandText = "EF_ShoppingCartGetTotalAmount";
		// create a new parameter
		DbParameter param = cmd.CreateParameter();
		param.ParameterName = "@CartID";
		param.Value = shoppingCartId;
		param.DbType = DbType.String;
		param.Size = 36;
		cmd.Parameters.Add(param);
		// return the result table
		return Decimal.Parse(GenericDataAccess.ExecuteScalar(cmd));
	}

	// Retrieve shopping cart items
	public static Int32 GetTotalItem()
	{
		// get a configured DbCommand object
		DbCommand cmd = GenericDataAccess.CreateCommand();
		// set the stored procedure name
		cmd.CommandText = "EF_ShoppingCartGetTotalItem";
		// create a new parameter
		DbParameter param = cmd.CreateParameter();
		param.ParameterName = "@CartID";
		param.Value = shoppingCartId;
		param.DbType = DbType.String;
		param.Size = 36;
		cmd.Parameters.Add(param);
		// return the result table
		return Int32.Parse(GenericDataAccess.ExecuteScalar(cmd));
	}

	// Create a new order from the shopping cart
	public static bool CreateOrder(EF_CustomerProperty customer)
	{
		// get a configured DbCommand object
		DbCommand cmd = GenericDataAccess.CreateCommand();
		// set the stored procedure name
		cmd.CommandText = "EF_CreateOrder";
		// create a new parameter
		DbParameter CartParam = cmd.CreateParameter();
		CartParam.ParameterName = "@CartID";
		CartParam.Value = shoppingCartId;
		CartParam.DbType = DbType.String;
		CartParam.Size = 36;
		cmd.Parameters.Add(CartParam);

		CartParam = cmd.CreateParameter();
		CartParam.ParameterName = "@CustomerName";
		CartParam.Value = customer.CustomerName;
		CartParam.DbType = DbType.String;
		cmd.Parameters.Add(CartParam);

		CartParam = cmd.CreateParameter();
		CartParam.ParameterName = "@CustomerPhone";
		CartParam.Value = customer.CustomerPhone;
		CartParam.DbType = DbType.String;
		cmd.Parameters.Add(CartParam);

		CartParam = cmd.CreateParameter();
		CartParam.ParameterName = "@CustomerEmail";
		CartParam.Value = customer.CustomerEmail;
		CartParam.DbType = DbType.String;
		cmd.Parameters.Add(CartParam);

		CartParam = cmd.CreateParameter();
		CartParam.ParameterName = "@ShippingAddress";
		CartParam.Value = customer.ShippingAddress;
		CartParam.DbType = DbType.String;
		cmd.Parameters.Add(CartParam);

		return GenericDataAccess.ExecuteCommand(cmd);
	}
}